<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of accessAction
 * 访问控制
 * @author xlp
 */
class accessAction extends commonAction {

    protected $authCacheFile = 'access/rule'; //权限控制缓存文件

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->adminGroup = C('admin', 'group');
        unset($this->adminGroup[1]);
    }

    function index() {
        $q = $this->_get('q');
        $type = $this->_get('st');
        $p = $this->_getid('p', 1);
        $group_id = $this->_getid('group', 0);
        $where = array();
        if ($group_id) {
            $where['is_public_action'] = ($group_id - 1);
        }
        if ($q && in_array($type, array('c_group', 'c_action', 'c_model'))) {
            $where['LIKE'] = array($type => parent::safeSearch($q));
        }
        $rs = M('admin_node')->where($where)->page($p)->order('nid DESC')->findAll();
        $total = M('admin_node')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'q' => $q, 'type' => $type, 'status' => array('否', '<span class="red">是</span>'), 'group_id' => $group_id));
        $this->display();
    }

    function add() {
        $this->display();
    }

    function post() {
//        z($_POST);
        $objData = array();
        $objData['c_group'] = $this->_post('c_group');
        $objData['n_group'] = $this->_post('n_group');
        $objData['c_action'] = $this->_post('c_action');
        $objData['n_action'] = $this->_post('n_action');
        $objData['is_public_action'] = $this->_postid('is_public_action', 0);
        $c_model = $this->_post('c_model');
        $n_model = $this->_post('n_model');
        //判断重复
        if (M('admin_node')->where(array('n_group' => $objData['n_group'], 'n_action' => $objData['n_action']))->find()) {
            $this->JsonReturn('该控制器已存在，请检查');
        }

        if (!$objData['is_public_action']) {
            if (!$c_model || !is_array($c_model) || !$n_model || !is_array($n_model)) {
                $this->JsonReturn('参数不正确');
            }
            $count = count($n_model);
            $c_modelObj = array();
            $n_modelObj = array();
            for ($i = 0; $i < $count; $i+=1) {
                if (isset($n_model[$i]) && $n_model[$i] && isset($c_model[$i]) && $c_model[$i]) {
                    $c_modelObj[] = $c_model[$i];
                    $n_modelObj[] = $n_model[$i];
                } else {
                    $this->JsonReturn('模块参数不正确');
                }
            }

            $inData = array();
            foreach ($n_modelObj as $key => $val) {
                $inData[] = array(
                    'c_group' => $objData['c_group'],
                    'n_group' => $objData['n_group'],
                    'c_action' => $objData['c_action'],
                    'n_action' => $objData['n_action'],
                    'c_model' => isset($c_modelObj[$key]) ? $c_modelObj[$key] : '',
                    'n_model' => $val,
                    'is_public_action' => 0
                );
            }
//        z($inData);
            M('admin_node')->insertAll($inData);
        } else {
            M('admin_node')->insert(array(
                'c_group' => $objData['c_group'],
                'n_group' => $objData['n_group'],
                'c_action' => $objData['c_action'],
                'n_action' => $objData['n_action'],
                'c_model' => '',
                'n_model' => '',
                'is_public_action' => 1
            ));
        }
        F($this->authCacheFile, null);
        $this->JsonReturn('ok', null, 1);
    }

    function edit() {
        $id = $this->_getid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('admin_node')->where(array('nid' => $id))->find();
        if (!$rs) {
            showError('模块不存在');
        }
//        z($rs);
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function save() {
        $id = $this->_postid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('admin_node')->where(array('nid' => $id))->find();
        if (!$rs) {
            showError('模块不存在');
        }
        $objData = array();
        $objData['c_group'] = $this->_post('c_group');
        $objData['n_group'] = $this->_post('n_group');
        $objData['c_action'] = $this->_post('c_action');
        $objData['n_action'] = $this->_post('n_action');
        $objData['c_model'] = $this->_post('c_model');
        $objData['n_model'] = $this->_post('n_model');
        $objData['is_public_action'] = $this->_postid('is_public_action', 0);
        if (!$objData['is_public_action'] && !$objData['n_model']) {
            $this->JsonReturn('模块参数不正确');
        }
//        z($objData);
        M('admin_node')->update($objData, array('nid' => $id));
        F($this->authCacheFile, null);
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        D('admin_node')->delete(array('nid' => $id));
        F($this->authCacheFile, null);
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 访问控制列表
     */

    function list_access() {
        $q = $this->_get('q');
        $type = $this->_get('st');
        $p = $this->_getid('p', 1);
        $group_id = $this->_getid('group', 0);
        $where = array();
        if ($q && in_array($type, array('c_group', 'c_action', 'c_model'))) {
            $where['LIKE'] = array($type => parent::safeSearch($q));
        }
        if ($group_id) {
            $where['group_id'] = ($group_id - 1);
        }
        $rs = M('admin_access')->where($where)->page($p)->order('id DESC')->findAll();
        $total = M('admin_access')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'q' => $q, 'type' => $type, 'group' => $this->adminGroup, 'group_id' => $group_id));
        $this->display();
    }

    /*
     * 添加访问控制
     */

    function add_access() {
        $rs = M('admin_node')->field('nid,c_group,n_group')->group('n_group')->findAll();
        $total = M('admin_node')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'group' => $this->adminGroup));
        $this->display();
    }

    function get_access_list() {
        $type = $this->_get('st');
        $q = $this->_get('q');
        if (!in_array($type, array('n_group', 'n_action'))) {
            showError('参数错误');
        }
        $group = '';
        if ($type == 'n_group') {
            $group = 'n_action';
        }
        $rs = M('admin_node')->where(array($type => $q, 'is_public_action' => 0))->group($group)->order('nid DESC')->findAll();
        $total = M('admin_node')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total));
        if ($type == 'n_group') {
            $this->JsonReturn('ok', $rs, 1);
        } else {
            $this->JsonReturn('ok', $this->getFetch('access_detail'), 1);
        }
    }

    function set_access() {
//        z($_POST);
        $model = $this->_post('model');
        $data = array(
            'groupid' => $this->_postid('groupid'),
            'n_group' => $this->_post('n_group'),
            'n_action' => $this->_post('n_action'),
        );
        if (!isset($this->adminGroup[$data['groupid']])) {
            $this->JsonReturn('用户组不存在');
        }
        if (!$data['n_group'] || !$data['n_action']) {
            $this->JsonReturn('分组及控制器不能为空');
        }
        if (!$model || !is_array($model)) {
            $this->JsonReturn('模块不正确');
        }
        $objData = array();
        foreach ($model as $val) {
            $mdata = explode('|', $val);
            if (!isset($mdata[1])) {
                $this->JsonReturn('参数不正确');
            }
            if (!M('admin_access')->field('id')->where(array('group_id' => $data['groupid'], 'node_id' => $mdata[1]))->find()) {
                $objData[] = array(
                    'group_id' => $data['groupid'],
                    'node_id' => $mdata[1],
                    'n_group' => $data['n_group'],
                    'n_action' => $data['n_action'],
                    'n_model' => $mdata[0],
                );
            }
        }
        if ($objData) {
//            z($objData);
            M('admin_access')->insertAll($objData);
            F($this->authCacheFile, null);
        }
        $this->JsonReturn('ok', null, 1);
    }

    function delete_access() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        D('admin_access')->delete(array('id' => $id));
        F($this->authCacheFile, null);
        $this->JsonReturn('ok', null, 1);
    }

    function get_model_list() {
        $group = $this->_post('group');
        $action = $this->_post('action');
        if (!checkPath($group)) {
            $this->JsonReturn('分组不能为空');
        }
        if (!checkPath($action)) {
            $this->JsonReturn('控制器不能为空');
        }
        $path = APP_PATH . 'lib/action/' . (($group == 'index' || !$group) ? 'index/' : $group . '/') . $action . 'Action.class.php';
        if (!class_exists($action . 'Action')) {
            if (is_file($path)) {
                include $path;
            } else {
                $this->JsonReturn('控制器不存在');
            }
        }
        $repArr = get_class_methods(get_parent_class($action . 'Action')); //获取父类方法
        $allArr = get_class_methods($action . 'Action'); //获取制定类方法
//        z($allArr);
        $arr = array_diff($allArr, $repArr); //取去除父类方法后的剩余public方法
        $list = array('index' => '首页列表', 'add' => '添加', 'post' => '保存添加',
            'edit' => '编辑', 'save' => '保存编辑', 'delete' => '删除', 'search' => '搜索',
            'update' => '更新', 'detail' => '详细', 'deal' => '处理数据', 'setting' => '设置', 'clear' => '清理');
        $objData = array();
        foreach ($arr as $val) {
            $objData[] = array(
                'name' => isset($list[$val]) ? $list[$val] : '',
                'model' => $val
            );
        }
        $this->JsonReturn('ok', $objData, 1);
    }

}
